﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Aulac.Rsc.Bss.Tags;
using Aulac.Client.Global.Interface;
using System.Windows.Forms;
using Aulac.Rsc.Bss;
using Aulac.Global.Common;
using Aulac.UtilityTools;

namespace Aulac.Client.Bss.WinForm.CapAss
{
    public partial class FormDlgProvider
    {
        #region Override
        public override void OnFormLoad()
        {
            base.OnFormLoad();
            this.AulacNavCtrl.ShowControlAll(false);
            this.AulacNavCtrl.ShowControl(true, AulacNavCtrl.DelControl, AulacNavCtrl.SaveControl, AulacNavCtrl.ExitControl);
            txtPrvdID.Focus();
            
        }

        protected override void OnSetStateControls(Aulac.Global.Enums.FormModes pFormMode)
        {
            base.OnSetStateControls(pFormMode);
            this.AulacNavCtrl.ShowControlAll(false);
            if (pFormMode == Aulac.Global.Enums.FormModes.LockMode || pFormMode == Aulac.Global.Enums.FormModes.ViewMode)
            {
                List<IAulacDataControl> mLstControls = null;

                GetListAulacDataControls(ref mLstControls, grBoxInfo);
                GetListAulacDataControls(ref mLstControls, groupBox1);
                foreach (IAulacStateCtrl ctr in mLstControls)
                    ctr.Enabled = false;
                this.AulacNavCtrl.SetStateControl(pFormMode);
            }
            else
            {
                List<IAulacDataControl> mLstControls = null;
                GetListAulacDataControls(ref mLstControls, grBoxInfo);
                GetListAulacDataControls(ref mLstControls, groupBox1);
                foreach (IAulacStateCtrl ctr in mLstControls)
                    ctr.Enabled = true;
            }
            if (pFormMode == Aulac.Global.Enums.FormModes.NewMode)
            {
                chkIsActive.Checked = true;                
            }                        
            btnNav.ShowButton(true, btnNav.btnSave, btnNav.btnExit, btnNav.btnDel);
            btnNav.WidthButton = 100;
            this.AulacNavCtrl.SetStateControl(pFormMode);
        }
        protected override bool OnShowDetailVoucher()
        {            
            return GetDetailItemCapAss();
        }
        protected override void OnAddNewVoucher()
        {
            ClearValueToControls();
            base.OnAddNewVoucher();
            txtPrvdID.Focus();
        }
        protected override void OnEditVoucher()
        {           
            base.OnEditVoucher();
            GetDetailItemCapAss();            
            txtPrvdID.Focus();
        }
        protected override bool OnBeginSaveVoucher()
        {
            if (txtPrvdID.IsEmpty)
            {
                MsgBox.Show("Mã Nhà cung cấp không được rỗng !", MsgBox.Title, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtPrvdID.Focus();
                return false;
            }
            if (txtPrvdName.IsEmpty)
            {
                Aulac.UtilityTools.MsgBox.Show("Tên nhà cung cấp không được rỗng !", "AulacSoft", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtPrvdName.Focus();
                return false;
            }
            return base.OnBeginSaveVoucher();
        }        
        protected override bool OnSaveVoucherData(ref System.Xml.XmlDataDocument xmlData)
        {
            return SaveItemProvider(ref xmlData);
        }
        protected override void OnEndSaveVoucher(Aulac.Global.Enums.FormModes pEndFormMode, System.Xml.XmlDataDocument xmlData)
        {
            base.OnEndSaveVoucher(pEndFormMode, xmlData);
        }
        protected override bool OnDeleteDataVoucher()
        {
            return DeleteProvider();
        }
        protected override bool OnBeginExit()
        {
            if (FormMode == Aulac.Global.Enums.FormModes.EditMode || FormMode == Aulac.Global.Enums.FormModes.NewMode)
                return true;
            else
                FormMode = Aulac.Global.Enums.FormModes.ViewMode;
            return true;
        }
        #endregion

        #region Method
        private void SetFieldMember()
        {
            // ---------------- SET FieldMember ------------------
            
            txtPrvdID.FieldMember = tagItems.Provider.ProviderID;
            txtNote.FieldMember = tagItems.Provider.Note;
            txtPrvdAddr.FieldMember = tagItems.Provider.ProviderAddr;
            txtPrvdDelegate.FieldMember = tagItems.Provider.ProviderDelegate;
            txtPrvdEmail.FieldMember = tagItems.Provider.ProviderEmail;
            txtPrvdName.FieldMember = tagItems.Provider.ProviderName;
            txtPrvdPhone.FieldMember = tagItems.Provider.ProviderPhone;
            chkIsActive.FieldMember = tagItems.Provider.IsActive;
        }

        /// <summary>
        /// Xóa dữ liệu các Controls trong Form.
        /// </summary>
        public void ClearValueToControls()
        {
            List<IAulacDataControl> mListControl = null;
            GetListAulacDataControls(ref mListControl, grBoxInfo);
            GetListAulacDataControls(ref mListControl, groupBox1);
            foreach (IAulacValueDataCtrl ctr in mListControl)
            {
                ctr.ClearValueControls();
            }
        }

        private void GetListAulacDataControls(ref List<IAulacDataControl> mListControl, System.Windows.Forms.GroupBox grpBox)
        {
            if (mListControl == null)
                mListControl = new List<IAulacDataControl>();
            foreach (Control ctr in grpBox.Controls)
            {
                if (ctr is IAulacDataControl)
                    mListControl.Add((IAulacDataControl)ctr);
            }
        }
        #endregion        

        #region Properties
        private string mProviderID = "";
        public string ProviderID
        {
            get
            { return mProviderID; }
            set
            { mProviderID = value; }
        }
        private string mProviderPrkID = "";
        public string ProviderPrkID
        {
            get
            { return mProviderPrkID; }
            set
            { mProviderPrkID = value; }
        }
        private string mTitleName = "";
        public string TitleName 
        {
            get
            { return mTitleName;}
            set
            { 
                mTitleName = value;                
                if (!Aulac.Global.Common.StringHelper.IsEmpty(mTitleName))
                {
                    this.lblTitle.Text = mTitleName;
                    this.Text = Aulac.Global.Common.StringHelper.ReplaceIndex(mTitleName.ToLowerInvariant(), mTitleName.Substring(0, 1).ToLower(), mTitleName.Substring(0, 1).ToUpper(), 0);
                }
            } 
        }
        #endregion
    }
}
